package cn.shiva.cloud.consumer.controller;

import cn.shiva.cloud.consumer.entity.Order;
import cn.shiva.cloud.consumer.mapper.OrderMapper;
import cn.shiva.common.basic.Result;
import cn.shiva.common.utils.CodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shiva   2021/10/20 21:15
 */
@Slf4j
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 下单
     */
    @ResponseBody
    @PostMapping(value = "create")
    public Result<Object> create(@RequestBody Order order) {
        int resultId = orderMapper.insert(order);
        if (resultId > 0) {
            return Result.builder().code(CodeEnum.SUCCESS.code).message("操作成功").data(resultId).build();
        } else {
            return Result.builder().code(CodeEnum.UNSATISFIED.code).message("操作失败").build();
        }
    }

    /**
     * 根据订单支付
     */
    @ResponseBody
    @PostMapping(value = "payByOrderId")
    public Result<Object> payByOrderId(Long id) {
        int resultId = 0;
        if (resultId > 0) {
            return Result.builder().code(CodeEnum.SUCCESS.code).message("操作成功").data(resultId).build();
        } else {
            return Result.builder().code(CodeEnum.UNSATISFIED.code).message("操作失败").build();
        }
    }


    /**
     * 根据ID获取订单
     */
    @ResponseBody
    @GetMapping(value = "getById")
    public Result<Order> getById(Long id) {
        Order order = orderMapper.selectById(id);
        if (order != null) {
            return Result.<Order>builder().code(CodeEnum.SUCCESS.code).message("查询成功").data(order).build();
        } else {
            return Result.<Order>builder().code(CodeEnum.UNSATISFIED.code).message("未查询到指定记录").build();
        }
    }


}
